package com.lzj.demo.design.controller;

import com.lzj.demo.design.door.annotation.IsBlackList;
import com.lzj.demo.design.dto.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/demo/user")
@Slf4j
public class UserController {
    //难道每个接口都要做一遍当前登录用户黑白名单校验吗？
    @PostMapping("/query")
    public UserInfo queryUserInfo(@RequestParam String userId){
        if(isBlackList(userId)){
            return new UserInfo("非白名单限制");
        }
        //todo 查询用户的逻辑 ...
        return new UserInfo(userId,"福建沙县");
    }

    //使用门面设计模式重构
    @PostMapping("/get")
    @IsBlackList(key = "userId", returnJson = "{\"code\":\"10000\",\"info\":\"非白名单可访问用户拦截\"}")
    public UserInfo getUserInfo(@RequestParam String userId){
        //todo 查询用户的逻辑 ...
        return new UserInfo(userId,"福建沙县");
    }

    boolean isBlackList(String userId){
        //白名单拦截
        List<String> userList = new ArrayList<>();
        userList.add("1001");
        userList.add("1002");
        userList.add("1003");
        if(!userList.contains(userId)){
            return true;
        }
        return false;
    }
}
